<?php

class Frontend_PostController extends Zend_Controller_Action {

    public function indexAction() {
        $objPost = new Application_Model_Posts();
        $arrPostList = $objPost->getPostList();
        $this->view->assign('arrPostList', $arrPostList);
    }

    public function detailsAction() {
        $intID = $this->getParam('id', 0);
        $objPost = new Application_Model_Posts();
        $objComment = new Application_Model_Comments();
        $arrDetailPost = $objPost->getDetailPost($intID);
        $arrDetailComment = $objComment->getPostComment($intID);
        $this->view->assign('arrPost', $arrDetailPost);
        $this->view->assign('arrCommentList', $arrDetailComment);
        //Count post view   
        $this->countPostView($intID,$arrDetailPost[0]['title']);
    }

    private function countPostView($intPostID,$strTitle) {
        $dbRedis = Zend_Registry::get('dbRedis');
        $dbRedis->incr($strTitle.'-'.$intPostID);
        $intTotalView = $dbRedis->get($strTitle.'-'.$intPostID);
        $dbRedis->zAdd('top_post',$intTotalView ,$strTitle.'-'.$intPostID);
        echo $intTotalView;
    }

    public function topAction() {
        $dbRedis = Zend_Registry::get('dbRedis');
        $arrTopPost = $dbRedis->zRevRange('top_post', 0, -1,true);          
        $this->view->TopPost = $arrTopPost;   
    }
}
